<template>
	<view class="container">
		<uni-section title="填写信息" type="line">
			<view class="example">
				<uni-forms ref="baseForm" :modelValue="baseFormData" :rules="rules">
					<uni-forms-item label="姓名" required name="name">
						<uni-easyinput v-model="baseFormData.name" placeholder="请输入姓名" />
					</uni-forms-item>
					<uni-forms-item label="手机号" required name="phone">
						<uni-easyinput v-model="baseFormData.phone" placeholder="请输入手机号" />
					</uni-forms-item>
					<uni-forms-item label="党支部" required name="party">
						<uni-easyinput v-model="baseFormData.party" placeholder="请输入党支部" />
					</uni-forms-item>
					<uni-forms-item label="入党时间" name="datetimesingle" required>
						<uni-datetime-picker type="date" return-type="timestamp"
							v-model="baseFormData.datetimesingle" />
					</uni-forms-item>
				</uni-forms>
			</view>
			<button type="primary" @click="submit()">{{ writed ? "修改并保存" : "提交" }}</button>
		</uni-section>
	</view>
</template>

<script>
	const db = uniCloud.database()
	let abu_user_info_db = db.collection('abu_user_info');
	export default {
		data() {
			return {
				user_info: {},
				writed: false,
				baseFormData: {
					name: '',
					phone: '',
					party: '',
					datetimesingle: 1627529992399
				},
				rules: {
					name: {
						required: true,
						message: '姓名不能为空'
					},
					phone: {
						required: true,
						format: 'number',
						message: '手机号不能为空且只能输入数字'
					},
					party: {
						required: true,
						message: '党支部不能为空'
					}
				}
			}
		},
		async onLoad() {
			this.user_info = await uniCloud.getCurrentUserInfo()
			if (this.user_info) {
				await this.load_data();
			} else {
				// 处理未获取到用户信息的情况
			}
		},
		onReady() {
			this.$refs.baseForm.setRules(this.rules);
		},
		methods: {
			async load_data() {
				const res = await abu_user_info_db.where({
					user_id: this.user_info.uid
				}).get()
				if (res.result.data.length > 0) {
					this.writed = true
					this.baseFormData = res.result.data[0];
				}
			},
			async submit() {
				try {
					await this.$refs.baseForm.validate()
					const { _id, ...form_data } = this.baseFormData;
					const userData = {
						...form_data,
						user_id: this.user_info.uid
					}
					if (this.writed) {
						console.log("doc",userData);
						await abu_user_info_db.where({
							user_id: userData._id
						}).update(userData)
					} else {
						console.log("add");
						await abu_user_info_db.add(userData)
					}
					uni.showToast({
						title: "提交成功",
						icon: 'none'
					});
					console.log("提交成功");
				} catch (error) {
					console.error('提交失败', error);
				}
			},
		}
	}
</script>

<style lang="scss">
	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>